var paper = {
		init : function() {
			paper.paperGrid = $('#paperGrid');
			paper.paperAddDialog = $('#paperAddDialog');
			paper.paperAddForm = $('#paperAddForm');
			paper.paperAccordAddDialog = $('#paperAccordAddDialog');
			paper.paperAccordAddForm = $('#paperAccordAddForm');
			paper.paperDetailsForm = $('#paperDetailsForm');
			paper.paperDetailsDialog = $('#paperDetailsDialog');
			paper.paperCourse=$('#paper-course');
			paper.paperName=$('#paper-name');
			paper.paperState=$('#paper-state');
			paper.addCourse=$('#add-paper-course');
			paper.addPaperName=$('#add-paper-name');
			paper.accordPaperName=$('#accord-paper-name');
			paper.accordCourse=$('#accord-paper-course');
			paper.paperPreviewDialog=$('#paperPreviewDialog');
			
			
			paper.paperCourse.combotree({
				url: window.ctx+'/QesExamPaperServlet?command=listMenu',
				lines: true,
				multiple:true,
				checkbox: true,
				onlyLeafCheck: true,
				onLoadSuccess: function(node, data) {
					$(this).tree('expandAll');
					$(this).find('span.tree-checkbox').unbind().click(function () {
						var treeObj=paper.paperCourse.combotree('tree');
						treeObj.tree('select', $(this).parent());
	                    return false;
	                });
				},
				onSelect: function (node) {
					var treeObj=paper.paperCourse.combotree('tree');
	                var cknodes = treeObj.tree("getChecked");
	                for (var i = 0; i < cknodes.length; i++) {
	                    if (cknodes[i].id != node.id) {
	                    	treeObj.tree("uncheck", cknodes[i].target);
	                    }
	                }
	                if (node.checked) {
	                	treeObj.tree('uncheck', node.target);

	                } else {
	                	treeObj.tree('check', node.target);

	                }

	            }
			});
			
			
			paper.paperGrid.datagrid({
			    url:window.ctx+'/QesExamPaperServlet?command=listpaper',
			    fixed:true,
			    fitColumns: true,
			    striped: true,
			    rownumbers: true,
			    pagination: true,
			    pageSize: 20,
			    pageList: [10, 20, 30, 40, 50],
			    pageNumber: 1,
			    toolbar: '#paperToolbar',
			    columns:[[
					{field:'id', width:100, checkbox: true},
					{field:'name', title:'试卷名称', width:100,fixed: true,align:'center'},
					{field:'course__text', title:'所属课程', fixed: true, width:100,  align:'center'},
					{field:'totalPoint', title:'总分分数', fixed: true,width:100, align:'center'},
					{field:'passPoint', title:'通过分数', fixed: true,width:100, align:'center'},
					{field:'totalMinutes', title:'总时间', fixed: true,width:150, align:'center'},
					{field:'stateStr', title:'状态',fixed: true, width:150, align:'center'}
			    ]],
				onDblClickRow: function(rowIndex, rowData) {
					paper.toolbar.showDetailsDialog(rowData.id);
				},
				/* onBeforeLoad: function (param) {
					
					param.paper=paper.paper.val();
			    }, */
				
			});
		},
		toolbar: {
			showAddDialog: function() {
				paper.toolbar.showAddCombotree();
				paper.paperAddDialog.dialog('open');
				paper.addPaperName.focus();
			},
			hiddenAddDialog: function() {
				paper.paperAddDialog.dialog('close');
				paper.toolbar.resetAddForm();
			},
			resetAddForm: function() {
				paper.paperAddForm.form('reset');
			},
			// 自动新增时
			showAccordAddDialog: function() {
				paper.toolbar.showAccordAddCombotree();
				paper.paperAccordAddDialog.dialog('open');
				paper.accordPaperName.focus();
			},
			hiddenAccordAddDialog: function() {
				paper.paperAccordAddDialog.dialog('close');
				paper.toolbar.resetAccordAddFrom();
			},
			resetAccordAddFrom: function() {
				paper.paperAccordAddForm.form('reset');
			},
			showAddCombotree:function(){
				paper.addCourse.combotree({
					url: window.ctx+'/QesExamPaperServlet?command=listMenu',
					lines: true,
					multiple: true,
					checkbox: true,
					onlyLeafCheck: true,
					onLoadSuccess: function(node, data) {
						$(this).tree('expandAll');
						$(this).find('span.tree-checkbox').unbind().click(function () {
							var treeObj=paper.addCourse.combotree('tree');
							treeObj.tree('select', $(this).parent());
		                    return false;
		                });
					},
					onSelect: function (node) {
						var treeObj=paper.addCourse.combotree('tree');
		                var cknodes = treeObj.tree("getChecked");
		                for (var i = 0; i < cknodes.length; i++) {
		                    if (cknodes[i].id != node.id) {
		                    	treeObj.tree("uncheck", cknodes[i].target);
		                    }
		                }
		                if (node.checked) {
		                	treeObj.tree('uncheck', node.target);
		                } else {
		                	treeObj.tree('check', node.target);
		                }
		            }
				});
			},
			showAccordAddCombotree:function(){
				paper.accordCourse.combotree({
					url: window.ctx+'/QesExamPaperServlet?command=listMenu',
					lines: true,
					multiple: true,
					checkbox: true,
					onlyLeafCheck: true,
					onLoadSuccess: function(node, data) {
						$(this).tree('expandAll');
						$(this).find('span.tree-checkbox').unbind().click(function () {
							var treeObj=paper.paperCourse.combotree('tree');
							treeObj.tree('select', $(this).parent());
		                    return false;
		                });
					},
					onSelect: function (node) {
						var treeObj=paper.paperCourse.combotree('tree');
		                var cknodes = treeObj.tree("getChecked");
		                for (var i = 0; i < cknodes.length; i++) {
		                    if (cknodes[i].id != node.id) {
		                    	treeObj.tree("uncheck", cknodes[i].target);
		                    }
		                }
		                if (node.checked) {
		                	treeObj.tree('uncheck', node.target);
		                } else {
		                	treeObj.tree('check', node.target);
		                }
		            }
				});
			},
			
			accordAdd: function() {
				if (paper.paperAccordAddForm.form('validate')) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx+'/QesExamPaperServlet?command=accordAddPaper', paper.paperAccordAddForm.serialize(), function(data) {
						if (data==1) {
							$.messager.show({
								title: '系统提示',
								msg: '新增成功！',
								timeout: 3000,
								showType: 'slide'
							});
							paper.toolbar.resetAccordAddFrom();
							paper.paperGrid.datagrid('reload');
							
						} else {
							$.messager.alert('系统提示', '新增失败！', 'error')
						}
						$.messager.progress('close');
						paper.toolbar.hiddenAccordAddDialog();
					});
				}
			},
			add: function() {
				if (paper.paperAddForm.form('validate')) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx+'/QesExamPaperServlet?command=addpaper', paper.paperAddForm.serialize(), function(data) {
						if (data==1) {
							$.messager.show({
								title: '系统提示',
								msg: '新增成功！',
								timeout: 3000,
								showType: 'slide'
							});
							paper.toolbar.resetAddForm();
							paper.paperGrid.datagrid('reload');
							paper.toolbar.hiddenAddDialog();
						}else if(data=='false'){
							$.messager.alert('系统提示', '新增失败！输入的分数非法', 'error')
						} else {
							$.messager.alert('系统提示', '新增失败！', 'error');
							paper.toolbar.hiddenAddDialog();
						}
						$.messager.progress('close');
						
					});
				}
			},
			showDetailsDialog: function(id) {
				var getData = function(v) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx+'/QesExamPaperServlet?command=getpaper', {id : v}, function(data) {
						if (data) {
							paper.paperDetailsForm.form('load', {
								id:					data.id,
								name: 				data.name,
								url:  				data.paperUrl,
								type: 				data.paperTypeStr,
								course:		 		data.course__text,
								totalPoint:			data.totalPoint,
								passPoint:			data.passPoint,
								totalMinutes:		data.totalMinutes,
								singleNumber:		data.singleOptionNumber,
								singlePoint:		data.singleOptionEachPoint,
								singleDifficulty:	data.singleOptionDifficultyStr,
								multiNumber:		data.multiOptionNumber,
								multiPoint:			data.multiOptionEachPoint,
								multiDifficulty:	data.multiOptionDifficultyStr,
								judgeNumber:		data.judgeNumber,
								judgePoint:			data.judgeEachPoint,
								judgeDifficulty:	data.judgeDifficultyStr,
								operateName:		data.user__name,
								operateTime:		data.opreateTime,
								state:				data.stateStr			
							});
							$('#paperDetailsDialog').dialog('open');
						} else {
							$.messager.alert('系统提示', '获取信息失败！', 'error')
						}
						$.messager.progress('close');
					}, 'json');
				}
				if (id) {
					getData(id);
				} else {
					var row = paper.paperGrid.datagrid('getSelections');
					if (row.length == 1) {
						getData(row[0].id);
					} else {
						$.messager.show({
							title: '系统提示',
							msg: '请选择一行数据进行修改！',
							timeout: 3000,
							showType: 'slide'
						});	
					}
				}
			},
			hiddenDetailsDialog: function() {
				$('#paperDetailsDialog').dialog('close');
				paper.toolbar.resetDetailsFrom();
			},
			resetDetailsFrom: function() {
				paper.paperDetailsForm.form('reset');
			},
			DetailspaperState: function() {
				var row = paper.paperGrid.datagrid('getSelections');
				if (row.length == 1) {
					var id;
					var state;
					$.each(row, function(i, v) {
						id=v.id;
						state=v.state;
					});
					$.post(window.ctx+'/QesExamPaperServlet?command=updatePaperState', {id: id,state: state}, function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '修改成功！',
								timeout: 3000,
								showType: 'slide'
							});
							paper.paperGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '修改失败！', 'error')
						}
						$.messager.progress('close');
					});
				} else {
					$.messager.show({
						title: '系统提示',
						msg: '请选择数据进行修改！',
						timeout: 3000,
						showType: 'slide'
					});	
				}
			},
			paperRebuildPaper:function(id){
				var getRebuildPaper = function(v) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx+'/QesExamPaperServlet?command=rebuildPaper', {id : v}, function(data) {
						if (data) {
							$.messager.show({
								title: '系统提示',
								msg: '生成成功！',
								timeout: 3000,
								showType: 'slide'
							});
							paper.paperGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '生成试卷失败！', 'error')
						}
						$.messager.progress('close');
					}, 'json');
				}
				if (id) {
					getRebuildPaper(id);
				} else {
					var row = paper.paperGrid.datagrid('getSelections');
					if (row.length == 1) {
						getRebuildPaper(row[0].id);
					} else {
						$.messager.show({
							title: '系统提示',
							msg: '请选择一行数据进行重新生成试卷！',
							timeout: 3000,
							showType: 'slide'
						});	
					}
				}
			},
			paperPreviewDialog: function(id) {
				var getData = function(v) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx+'/QesExamPaperServlet?command=previewPaper', {id : v}, function(data) {
						if (data != 'false' && data) {
							var jsonObj=$.parseJSON(data);
							var singleList=jsonObj.singleList;
							var multipleList=jsonObj.multipleList;
							var judgeList=jsonObj.judgeList;						
							var Options =[];
							
							Options.push('<div style="text-align:center;width:100%;"><b style="font-size:48px">'+jsonObj.name+'</b>');
							Options.push('<h4>(时间：'+jsonObj.totalMinutes +',分数:'+jsonObj.totalPoint+')</h4></div>');
							
							if(singleList.length != 0){
								Options.push('<div><b style="font-size:24px">一、单选题  </b>(共'+jsonObj.singleOptionNumber+'题 X'+jsonObj.singleOptionEachPoint+'分)</div><div style="margin-left:30px">');
							for(var i=0; i<singleList.length; i++){														
								Options.push('<p><b>'+''+(i+1)+'.'+singleList[i].question +'</b>('+singleList[i].answer+')</p>');
								Options.push('<p><b>'+'A.'+singleList[i].answerA +'</b></p>');
								Options.push('<p><b>'+'B.'+singleList[i].answerB +'</b></p>');
								Options.push('<p><b>'+'C.'+singleList[i].answerC +'</b></p>');
								Options.push('<p><b>'+'D.'+singleList[i].answerD +'</b></p>');	
							}
							Options.push('</div>');
							}
							
							if(multipleList.length != 0){
								Options.push('<div><b style="font-size:24px">二、多选题  </b>(共'+jsonObj.multiOptionNumber+'题 X'+jsonObj.multiOptionEachPoint+'分)</div><div style="margin-left:30px">');
							for(var i=0; i<multipleList.length; i++){							
								Options.push('<p><b>'+''+(i+1)+'.'+multipleList[i].question +'</b>('+multipleList[i].answer+')</p>');
								Options.push('<p><b>'+'A.'+multipleList[i].answerA +'</b></p>');
								Options.push('<p><b>'+'B.'+multipleList[i].answerB +'</b></p>');
								Options.push('<p><b>'+'C.'+multipleList[i].answerC +'</b></p>');
								Options.push('<p><b>'+'D.'+multipleList[i].answerD +'</b></p>');	
							}
							Options.push('</div>');
							}
							if(judgeList.length != 0){
								console.info(judgeList);
								Options.push('<div><b style="font-size:24px">三、判断题  </b>(共'+jsonObj.judgeNumber+'题 X'+jsonObj.judgeEachPoint+'分)</div><div style="margin-left:30px">');
							for(var i=0; i<judgeList.length; i++){							
								Options.push('<p><b>'+''+(i+1)+'.'+judgeList[i].question +'</b>('+judgeList[i].answer+')</p>');
							}
							Options.push('</div>');
							}
							paper.paperPreviewDialog.append(Options.join('')); 
							$('#paperPreviewDialog').dialog('open');
						} else {
							$.messager.alert('系统提示', '试卷丢失！请重新生成', 'error')		
						}
						$.messager.progress('close');
					}, 'json');
				}
				if (id) {
					getData(id);
				} else {
					var row = paper.paperGrid.datagrid('getSelections');
					if (row.length == 1) {
						getData(row[0].id);
					} else {
						$.messager.show({
							title: '系统提示',
							msg: '请选择一行数据进行修改！',
							timeout: 3000,
							showType: 'slide'
						});	
					}
				}
			},
			hiddenPreviewDialog: function() {
				paper.paperPreviewDialog.html('');
				$('#paperPreviewDialog').dialog('close');
				
			},
			redo: function() {
				paper.paperGrid.datagrid('unselectAll');
			},
			search:function(){
				var treeObj=paper.paperCourse.combotree('tree');
				var checked=treeObj.tree('getChecked');
				var id;
				$.each(checked,function(i,v){
					id=v.id;
				});
				paper.paperGrid.datagrid('load',{
					paperName: paper.paperName.val(),
					courseId: id,
					state: paper.paperState.val()
				});
				paper.paperGrid.datagrid('reload');
			}
			
		}
};

	$(function(){
		paper.init();
	});
	
